package org.his.controller.basicdata.yaopin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.his.dao.YpglDao;
import org.his.pojo.yaopin.Ypgl;
import org.his.utils.PinYin4jUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/ypgl")
public class YpglController {
        @Autowired
        private YpglDao ypglDao;

        @RequestMapping("/index")
        public String index() throws Exception {
            return "basicdata/yaopin/index";
        }

        @RequestMapping("/form")
        public String form() throws Exception {
            return "basicdata/yaopin/form";
        }

        @RequestMapping("/list")
        @ResponseBody
        public Map<String,Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, Ypgl condition) throws Exception{
            Map<String, Object> map = new HashMap<>();
            Page<Ypgl> pager = PageHelper.startPage(page, limit);

            map.put("data", ypglDao.getList(condition));
            map.put("count", pager.getTotal());
            map.put("code", 0);
            map.put("msg", null);
            return map;
        }

        @RequestMapping("/batchDelete")
        @ResponseBody
        public Map<String,Object> batchDelete(Integer ids) throws Exception{
            Map<String,Object> map = new HashMap<>();
            ypglDao.delete(ids);
            return map;
        }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> add(Ypgl ypgl) throws Exception{
        Map<String,Object> map = new HashMap<>();
        ypgl.setShouzimu(PinYin4jUtil.getPinYinHeadChar(ypgl.getYpname()));
        ypgl.setPinyin(PinYin4jUtil.getPingYin(ypgl.getYpname()));
        ypglDao.add(ypgl);
        return map;
    }

    @RequestMapping(value = "edit",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object>edit(Ypgl ypgl) throws Exception{
        Map<String,Object>map=new HashMap<>();
        ypgl.setShouzimu(PinYin4jUtil.getPinYinHeadChar(ypgl.getYpname()));
        ypgl.setPinyin(PinYin4jUtil.getPingYin(ypgl.getYpname()));
        ypglDao.update(ypgl);
        return map;
    }

    @RequestMapping("/view")
    @ResponseBody
    public Ypgl view(Integer ypid) throws Exception{
        return ypglDao.get(ypid);
    }
}
